<?php
namespace Weixin\Controller;
use Think\Controller;
class TokenController extends Controller {

	//令牌检测
	public function _initialize()
	{
		/*
		if(empty(session('uid'))){
			//$html = I('html','index');
			//$this->redirect('/Home/Login/index/html/'.$html);
			session('uid',1);
		}*/
	}

	//检测个人信息是否完善
	public function check_user(){
		$user = $this->userinfo(array('username','code'));
		if(!$user['username'] || !$user['code']){
			exit($this->redirect('/Home/User/usercode'));
		}
	}

	public function ajaxReturn($data, $type = '', $json_option = 0){
		header("Access-Control-Allow-Origin:*"); 
		go_log($data);
		parent::ajaxReturn($data,$type,$json_option);
	}

  protected function wx_authed_s($html){
	//p($html);die;
    D("UserInTimes")->add(['user_id'=>uid(),'time'=>current_time(),'html'=>$html]);
  	$user=D("User")->find(uid());

  	//如果是index, 吧之前的随机缓存干掉
  	if ($html != 'index') {
		S('user_'.uid().'_category_',[]);
		$ids=D("Category")->getField('id',true);
		foreach ($ids as  $id) {
			S('user_'.uid().'_category_'.$id,[]);
		}
  	}
  	  	//处理天数问题
  	if ($user['day']==date('d')) {
  		D("User")->where(['id'=>uid()])->save(['chance'=>6,'day'=>date('d')]);
  	}
	

  	if ($html == 'user' || $html == 'public') {
	  	$agreed = $user['agreed'];
	  	if (!$agreed) {
	  		$this->redirect('/home/user/agree',['html'=>$html]);
	  	}
  	}
  	//没有填写二维码或者微信号,到不了添加页面
  	if ($html == 'public' && (!$user['username'] || !$user['code'])) {
  		//redirect('/html2/user.html');
  		redirect('/User/user');
  	}
    if ($html == 'rank') {
      $this->redirect('/home/user/rank');   
    }

  	redirect('/User/'.$html.'.html');
  	//redirect('/html2/'.$html.'.html');
  }


	

	//用户信息
	public function userinfo($field=array(),$uid=''){
		$uid  = $uid ? : session('uid');
		$user = M('user')->field($field)->where(['id'=>$uid])->find();
		if($user){
			return $user;
		}else{
			//p($_SESSION);die;
			$this->error('用户不存在');
		}
	}

	//文件上传
	public function uploadtu($dir='',$names=''){ 
		header("Content-type: text/html; charset=utf-8");
		//$names = $names ? : time().'_'.mt_rand();
		$names = $names ? : uniqid;
		$upload = new \Think\Upload();
		$upload->replace   = true;
		$upload->maxSize   = 1500000 ;// 设置附件上传大小 
		$upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
		$upload->savePath  = $dir;   
		$upload->saveName  = iconv('utf-8', 'gb2312', $names);
		$info   =   $upload->upload();    
		if(!$info) {
			// 上传错误提示错误信息        
			$this->error($upload->getError());    
		}else{
			if(count($info)>=1){
				return $info;
			}else{
				return $info['photo']['savepath'].$info['photo']['savename'];
			}
			//p($info);die;
			
		}
	}

	//End
}
